const {Router} = require('express')
let router = new Router()
let bodyParser = require("body-parser");
const likeModel = require('../models/likeModel')

router.post('/like',bodyParser.json(),async (req,res) => {
    const { aid,user_id } =req.body
    let likeInfo = await likeModel.find({aid,user_id})
    if(likeInfo) {
        res.send({success:true,data:likeInfo})
    }else{
        res.send({success:false,message:'查询失败'})
    }
})

router.post('/like/updatelike',bodyParser.json(),async (req,res) => {
    const { aid,user_id,status } =req.body
    const likeInfo = await likeModel.find({aid,user_id})
    const AllLikeInfo = await likeModel.find({})
    let id = 'l0000'+(AllLikeInfo.length+1)
    if(likeInfo.length) {
        const updateLike = await likeModel.updateOne({aid,user_id},{$set:{'status':status}})
        if(updateLike){
            res.send({success:true,message:'修改成功',data:updateLike})
        }else{
            res.send({success:false,message:'修改失败'})
        }
    }else{
        const updateLike = await likeModel.create({id,user_id,aid,status})
        if(updateLike){
            res.send({success:true,message:'插入数据成功',data:updateLike})
        }else{
            res.send({success:false,message:'插入数据失败'})
        }
    }
})

router.post('/like/sololike',bodyParser.json(),async (req,res) => {
    const { aid,user_id,status } =req.body
    let likeInfo = await likeModel.updateOne({aid,user_id},{$set:{'status':status}})
    if(likeInfo) {
        res.send({success:true,message:'修改成功',data:likeInfo})
    }else{
        res.send({success:false,message:'修改失败'})
    }
})

module.exports = router